package objects 
{
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import objects.HealthBar.HealthBar;
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Sprite;
	
	/**
	 * ...
	 * @author ...Vượng+Tuấn
	 */
	public class MyObject extends Sprite 
	{
		public var m_iLife:Boolean;
		public var m_id:int;
		public var m_hp:Number;
		public var m_fullHp : Number;
		public var m_damage:Number;
		public var m_armor:int;
		public var m_maxVelocity:Number;
		public var m_collision:Boolean;
		public var m_spriteMovie:MovieClip;
		public var m_spriteImage:Image;
		public var m_pos:Point;
		public var m_targetPos:Point;
		public var m_coolDown:Number;
		public var m_countAttack:int;
		public var m_velocity : Point;
		public var m_level : Number;

		public var m_animationState : int;
		public var m_movieVector:Vector.<MovieClip>;
		public var m_choosedEnemy :MyObject;
		public var m_newAnimationState : int ;
		public var m_percentDamage : Number;
		public var m_minDamage : Number;
		public var m_maxDamage : Number;
		public var m_healthBar : HealthBar;
		public var m_healthBarRed : HealthBar;
		public var m_coinEnemy : Number;
		public var m_EnemyDestination : Boolean;
		
		public function MyObject() 
		{
			m_iLife = false;
			m_id = 0;
			m_coinEnemy = 0;
			m_hp = 0;
			m_damage = 0;
			m_armor = 0;
			m_maxVelocity = 0;
			m_collision = false;
			m_spriteMovie = null;
			m_spriteImage = null;
			m_pos = new Point(0,0);
			m_targetPos = new Point(0, 0);
			m_coolDown = 0;
			m_countAttack = 0;
			m_level = 1;
			
		}
		public function TestCollisionelip(_enemy:MyObject, _circle:Image):Boolean
		{
			var a:Number;
			var b:Number;
			var c:Number;
			var d:Number;
			var X:Number;
			var Y:Number;
			var Y1_enemy_after:Number;
			var Y2_enemy_after:Number;
			var X1_enemy_after:Number;
			var X2_enemy_after:Number;
			var A:Number;
			
			a = _circle.width / 2;
			b = _circle.height / 2;
			
			c = _enemy.m_pos.x - _enemy.width / 2 - this.m_pos.y;
			Y = 0;
			Y1_enemy_after=_enemy.m_pos.y-_enemy.height/2-this.m_pos.y;//y duoi
			Y2_enemy_after=_enemy.m_pos.y+_enemy.height/2-this.m_pos.y;//y tren
			A=(1-(c*c)/(a*a))*b*b;

			if(  A >=0 )
			{

				Y=Math.sqrt(A);
				if((Y<Y2_enemy_after&&Y>Y1_enemy_after)||(-Y<Y2_enemy_after&&-Y>Y1_enemy_after)||(Y1_enemy_after>-Y&&Y2_enemy_after<Y))
					return true;
			}

			//canh doc ngoai cung
			c= _enemy.m_pos.x+_enemy.width/2-this.m_pos.x;
			Y=0;//giao diem
			Y1_enemy_after=_enemy.m_pos.y-_enemy.height/2-this.m_pos.y;//y duoi
			Y2_enemy_after=_enemy.m_pos.y+_enemy.height/2-this.m_pos.y;//y tren
			A=(1-(c*c)/(a*a))*b*b;

			if(  A >=0 )
			{

				Y=Math.sqrt(A);
				if((Y<Y2_enemy_after&&Y>Y1_enemy_after)||(-Y<Y2_enemy_after&&-Y>Y1_enemy_after)||(Y1_enemy_after>-Y&&Y2_enemy_after<Y))
					return true;
			}
			// canh ngang tren
			d= _enemy.m_pos.y+_enemy.height/2-this.m_pos.y;
			X=0;//giao diem
			X1_enemy_after=_enemy.m_pos.x-_enemy.width/2-this.m_pos.x;//x trai
			X2_enemy_after=_enemy.m_pos.x+_enemy.width/2-this.m_pos.x;//x phai
			A=(1-(d*d)/(b*b))*a*a;

			if(  A >=0 )
			{

				X=Math.sqrt(A);
				if((X<X2_enemy_after&&X>X1_enemy_after)||(-X<X2_enemy_after&&-X>X1_enemy_after)||(X1_enemy_after>-X&&X2_enemy_after<X))
					return true;
			}
			// canh ngang duoi
			d= _enemy.m_pos.y-_enemy.height/2-this.m_pos.y;
			X=0;//giao diem
			X1_enemy_after=_enemy.m_pos.x-_enemy.width/2-this.m_pos.x;//x trai
			X2_enemy_after=_enemy.m_pos.x+_enemy.width/2-this.m_pos.x;//x phai
			A=(1-(d*d)/(b*b))*a*a;

			if(  A >=0 )
			{

				X=Math.sqrt(A);
				if((X<X2_enemy_after&&X>X1_enemy_after)||-X<X2_enemy_after&&-X>X1_enemy_after||(X1_enemy_after>-X&&X2_enemy_after<X))
					return true;
			}
							
			return false;
		}
		public function TestCollisionelip_Point(_enemy:Point, _circle:Image):Boolean
		{
			var a:Number;
			var b:Number;
			var c:Number;
			var d:Number;
			var X:Number;
			var Y:Number;
			var Y1_enemy_after:Number;
			var Y2_enemy_after:Number;
			var X1_enemy_after:Number;
			var X2_enemy_after:Number;
			var A:Number;
			
			a = _circle.width / 2;
			b = _circle.height / 2;
			
			c = _enemy.x - this.m_pos.x;
			Y = 0;
			Y1_enemy_after=_enemy.y-this.m_pos.y;//y duoi
			Y2_enemy_after=_enemy.y-this.m_pos.y;//y tren
			A=(1-(c*c)/(a*a))*b*b;

			if(  A >=0 )
			{

				Y=Math.sqrt(A);
				if((Y<Y2_enemy_after&&Y>Y1_enemy_after)||(-Y<Y2_enemy_after&&-Y>Y1_enemy_after)||(Y1_enemy_after>-Y&&Y2_enemy_after<Y))
					return true;
			}

			//canh doc ngoai cung
			c= _enemy.x-this.m_pos.x;
			Y=0;//giao diem
			Y1_enemy_after=_enemy.y-this.m_pos.y;//y duoi
			Y2_enemy_after=_enemy.y-this.m_pos.y;//y tren
			A=(1-(c*c)/(a*a))*b*b;

			if(  A >=0 )
			{

				Y=Math.sqrt(A);
				if((Y<Y2_enemy_after&&Y>Y1_enemy_after)||(-Y<Y2_enemy_after&&-Y>Y1_enemy_after)||(Y1_enemy_after>-Y&&Y2_enemy_after<Y))
					return true;
			}
			// canh ngang tren
			d= _enemy.y-this.m_pos.y;
			X=0;//giao diem
			X1_enemy_after=_enemy.x-this.m_pos.x;//x trai
			X2_enemy_after=_enemy.x-this.m_pos.x;//x phai
			A=(1-(d*d)/(b*b))*a*a;

			if(  A >=0 )
			{

				X=Math.sqrt(A);
				if((X<X2_enemy_after&&X>X1_enemy_after)||(-X<X2_enemy_after&&-X>X1_enemy_after)||(X1_enemy_after>-X&&X2_enemy_after<X))
					return true;
			}
			// canh ngang duoi
			d= _enemy.y-this.m_pos.y;
			X=0;//giao diem
			X1_enemy_after=_enemy.x-this.m_pos.x;//x trai
			X2_enemy_after=_enemy.x-this.m_pos.x;//x phai
			A=(1-(d*d)/(b*b))*a*a;

			if(  A >=0 )
			{

				X=Math.sqrt(A);
				if((X<X2_enemy_after&&X>X1_enemy_after)||-X<X2_enemy_after&&-X>X1_enemy_after||(X1_enemy_after>-X&&X2_enemy_after<X))
					return true;
			}
							
			return false;
		}
		public function Collision(_object:MyObject, _time:Number):void
		{
			
		}
		
		public function update(_dt:Number):void
		{
			
		}
		
		public function getRect():Rectangle
		{
			return new Rectangle(0, 0, 0, 0);
		}
		public function isTouch(_point:Point):void
		{
			
		}
		public function ccpMult ( point : Point, s: Number) : Point
		{
			return new Point (point.x * s, point.y * s);
		}
		public function ccpNormalize ( v : Point) : Point
		{
			return  this.ccpMult(v, 1 / v.length) ;
		}
		public function ccpDot ( v1 : Point, v2 : Point) : Number
		{
			return v1.x * v2.x + v1.y * v2.y ;
		}
		public function ccpSub ( v1 : Point, v2 : Point) : Point
		{
			return new Point ( v1.x - v2.x, v1.y - v2.y );
		}
		public function ccpAdd ( v1 : Point, v2 : Point) : Point
		{
			return new Point ( v1.x + v2.x, v1.y + v2.y);
		}
		
		//Calculates the square length of a CCPoint (not calling sqrt() )
		public function ccpLengthSQ (v:Point) : Number
		{
			return this.ccpDot (v, v);
		}
		public function ccpLength ( v: Point) : Number
		{
			return Math.sqrt(this.ccpLengthSQ(v));
		}
		public function randomRange (min : Number, max: Number) : Number {
			return Math.floor ( Math.random() * (1 + max - min) + min);
		}
		
		public function release():void {
			
		}
	}
}